package com.liusy.serachengine.parserFile;

import java.io.File;


import org.apache.log4j.Logger;


public class ParserZip {
	private static Logger log = Logger.getLogger(ParserZip.class);
	private static StringBuffer content = new StringBuffer("");

	public static StringBuffer readzipFile(String path, String filePath) {
		readZip(path, filePath);
		MyUtils.delFiles(new File(filePath + "\\zip\\"));
		//System.out.println(content.toString());
		return content;
	}

	/**
	 * 
	 * @param path
	 *            �ļ�
	 * @param filePath
	 *            �ļ�Ŀ¼
	 * @return
	 */
	private static void readZip(String path, String filePath) {
		boolean flag = false;
		File file = new File(path);
		//if (file.canRead()) {
			if (file.isDirectory()) {
				String[] files = file.list();
				if (files != null) {
					for (int i = 0; i < files.length; i++) {
						readZip(filePath + "\\"+files[i], filePath+"\\"+files[i]);
					}
				}
			} else {
				
				String prefixNew = path.substring(path.lastIndexOf(".") + 1,
						path.length()).toLowerCase();
				String strFile = path;
				if (prefixNew.equals("html") || prefixNew.equals("htm")
						|| prefixNew.equals("mht")) {
					content.append(ParserHtml.readHtml(strFile).toString());
				} else if (prefixNew.equals("docx") || prefixNew.equals("doc")) {
					content.append(ParserWord.readWord(strFile).toString());
				} else if (prefixNew.equals("xlsx") || prefixNew.equals("xls")) {
					content.append(ParserExcel.readXls(strFile).toString());
				} else if (prefixNew.equals("ppt")) {
					content.append(ParserPpt.readPpt(strFile).toString());
				} else if (prefixNew.equals("pdf")) {
					content.append(ParserPdf.parserPDF(strFile).toString());
				} else if (prefixNew.equals("txt")) {
					content.append(ParserText.readTxt(strFile).toString());
				} else if (prefixNew.equals("zip")) {
					if(path.substring(0, path.lastIndexOf("\\")).indexOf("zip")==-1)//����ѹ���ļ��е�ѹ���ļ�����
					{
					flag = MyUtils.decompressionZipFiles(path, path.substring(0, path.lastIndexOf("\\"))
							+ "\\zip\\");
					if (flag)
						readZip(path.substring(0, path.lastIndexOf("\\"))
								+ "\\zip\\", path.substring(0, path.lastIndexOf("\\"))
								+ "\\zip\\");
					}
				} else if (prefixNew.equals("rar")) {
					if(path.substring(0, path.lastIndexOf("\\")).indexOf("ara")==-1)//����ѹ���ļ��е�ѹ���ļ�����
					{
					flag = MyUtils.decompressionRarFiles(path, path.substring(0, path.lastIndexOf("\\"))
							+ "\\zip\\");
					if (flag)
						readZip(path.substring(0, path.lastIndexOf("\\"))
								+ "\\zip\\", path.substring(0, path.lastIndexOf("\\"))
								+ "\\zip\\");
					}
				}   else if (prefixNew.equals("eml")) {
					content.append(ParserEml.readEmlFile(filePath).toString());
	             }
			}
		//}
	}

}
